﻿using System;
using System.Collections.Generic;
using System.Text;
using ULTCONFIGURATIONLib;
using Portal.Interface;
using Portal.Entity;
using System.Data;
using MyLib;
using System.Web;
using Portal.Logic;
using Workflow.Logic;

namespace UltimusV7.Logic
{
    public class UltimusOrgLogic:DBOrgLogic , IResource
    {
        public override bool CheckUser(string loginName, string password)
        {
            Ultimus.OC.OrgChart oc = new Ultimus.OC.OrgChart();
            bool flag = oc.VerifyUser(loginName.Trim(), password);
            return flag;
        }

        static List<string> _domains = new List<string>();
        public override List<string> GetDomains()
        {
            if (_domains.Count > 0)
            {
                return _domains;
            }
            byte num1;
            string[] array1;
            int num2;
            Array array2;
            int num3;
            num1 = 0;
            array1 = null;
            num2 = 0;

            ConfigureClass oConfig = new ConfigureClass();
            array2 = ((Array)oConfig.GetDomains(num1, num2));
            if (array2.Length > 0)
            {
                array1 = new string[array2.Length];
                for (num3 = 0; (num3 < array2.Length); num3 = (num3 + 1))
                {
                    array1[num3] = array2.GetValue(num3).ToString();

                }
            }
            _domains.AddRange(array1);
            return _domains; ;
        }
        
        public override UserEntity GetUserEntity(string loginName)
        {
            if (HttpContext.Current.Session["UserEntity"] != null)
            {
                return HttpContext.Current.Session["UserEntity"] as Portal.Entity.UserEntity;
            }
            UserEntity ety = new UserEntity();
            Ultimus.OC.OrgChart oc = new Ultimus.OC.OrgChart();
            Ultimus.OC.User user = new Ultimus.OC.User();
            oc.FindUser(loginName.Replace("\\","/"), "", 0, out user);

            ety.LOGINNAME = loginName;
            ety.CNNAME = user.strUserFullName;
            ety.ENNAME = user.strUserFullName;
            ety.USERID = user.nUserID;
            ety.JOBFUNCTION = user.strJobFunction;
            //ety.EXT01 = user.strDepartmentName; 
            ety.EXT02 = user.strDepartmentName; //ext01:当前部门名称
            HttpContext.Current.Session["UserEntity"] = ety;
            return ety;
        }

        static List<ResourceEntity> _list = null;
        public virtual List<ResourceEntity> GetResourceList()
        {
            if (_list == null)
            {
                ResourceLogic res = new ResourceLogic();
                if (res.GetKeyValue("EnableCustomOC") != "1")
                {
                    _list = GetOrgList();
                }
                else
                {
                    _list = new List<ResourceEntity>();
                }
            }
            return _list;
        }

        public virtual void ClearResourceList()
        {
            _list = new List<ResourceEntity>();
        }
    }
}
